<template>
  <div class="add-form">
    <el-dialog :title="titleInfo.text+titleInfo.pageTitle" :visible.sync="dialogFormVisible">
      <el-form
        :rules="ruleInline"
        ref="dataForm"
        :model="formBase"
        label-position="left"
        label-width="150px"
        style="width: 80%; margin-left:10px;"
      >
        <el-form-item v-if="this.$route.query.id ? false : true" label="所属学科" prop="subjectName">
          <el-select  placeholder="请选择频道" v-model="formBase.subjectID" @change="handleProvince">
              <el-option
              v-for=" item in subjList"
              :key="item.id"
              :label="item.subjectName"
              :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item  label="目录名称" prop="directoryName">
          <el-input v-model="formBase.directoryName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormH">{{$t('table.cancel')}}</el-button>
        <el-button type="primary" @click="createData">{{$t('table.confirm')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { list } from '@/api/hmmm/subjects'
import { add, update } from '@/api/hmmm/directorys.js'
export default {
  name: 'directorysAdd',
  props: {
    titleInfo: {
      type: Object,
      required: true
    },
    formBase: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      subID: '',
      subjList: [],
      dialogFormVisible: false,
      // 表单验证
      ruleInline: {
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  methods: {
    handleProvince () {

    },
    // 弹层显示
    dialogFormV () {
      this.dialogFormVisible = true
    },
    // 弹层隐藏
    dialogFormH () {
      this.dialogFormVisible = false
    },
    // 表单提交
    createData () {
      this.$refs.dataForm.validate(async valid => {
        if (this.$route.query.id) {
          this.formBase.subjectID = Number(this.$route.query.id)
        }
        if (valid) {
          this.dialogFormH()
          const data = {
            ...this.formBase
          }
          if (this.formBase.id) {
            await update(data).then(() => {
              this.$emit('newDataes', this.formBase)
            })
          } else {
            await add(this.formBase).then(() => {
              this.$emit('newDataes', this.formBase)
            })
          }
        } else {
          this.$message.error('*号为必填项!')
        }
      })
    },
    // 获取学科列表
    async getSubjList () {
      try {
        const { data: res } = await list()
        console.log(res)
        this.subjList = res.items
      } catch (err) {
        console.log(err)
      }
    }
  },
  // 挂载结束
  mounted: function () {},
  // 创建完毕状态
  created () {
    if (this.$route.query.id == null) {
      this.getSubjList()
    }
    // this.formBase
  },
  // 组件更新
  updated: function () {}
}
</script>
<style>
.el-form--label-left .el-form-item__label {
  text-align: right;
}
.el-form-item--medium {
  margin-bottom: 22px;
}
.el-dialog__footer {
  text-align: center;
}
</style>
